package sh.wn36.vehiclemanage.controller.cjl_controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import sh.wn36.vehiclemanage.entity.*;
import sh.wn36.vehiclemanage.entity.entity_cjl.Maintain;
import sh.wn36.vehiclemanage.entity.vo.MaintainQuery;
import sh.wn36.vehiclemanage.service.CarService;
import sh.wn36.vehiclemanage.service.CurrentUnitService;
import sh.wn36.vehiclemanage.service.DictionaryService;
import sh.wn36.vehiclemanage.service.DriverService;
import sh.wn36.vehiclemanage.service.service_cjl.MaintainRecordService;
import sh.wn36.vehiclemanage.util.Result;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/MaintainRecord")
@Api("车辆保养记录模块接口")
public class MaintainController {

    @Autowired
    private MaintainRecordService maintainRecordService;

    @Autowired
    private CarService carService;

    @Autowired
    private DriverService driverService;

    @Autowired
    private DictionaryService dictionaryService;

    @Autowired
    private CurrentUnitService currentUnitService;


    @ApiOperation(value = "按条件分页查询 保养记录")
    @PostMapping("/listMaintain/{current}/{limit}")
    public Result listMaintainByPage(@PathVariable("current")Integer current, @PathVariable("limit")Integer limit, @RequestBody(required = false) MaintainQuery maintainQuery){
        Page<Maintain> maintainPage = new Page<>(current,limit);
        QueryWrapper<Maintain> wrapper = new QueryWrapper<>();
        String carNo = maintainQuery.getCarNo();
        if(maintainQuery != null) {
            if (!StringUtils.isEmpty(carNo)) {
                wrapper.like("c.car_no", carNo);
            }
        }
        IPage<Maintain> maintain = maintainRecordService.listMaintainByPage(maintainPage, wrapper);
        List<Maintain> records = maintain.getRecords();
        long total = maintainPage.getTotal();
        return Result.OK().data("item",records).data("total",total);
    }

    @ApiOperation(value = "根据id删除保养表记录")
    @GetMapping("/removemMintain/{id}")
    public Result removeMintainById(@PathVariable("id") Integer id){
        boolean b = maintainRecordService.removeById(id);
        if(b){
            return Result.OK();
        }else {
            return Result.Fail();
        }
    }

    @GetMapping("/findAllMaintainNeed")
    public Result findAllMaintainNeed(){
        List<Car> carList = carService.list();
        List<Driver> driverList = driverService.list();
        List<Dictionary> dictionaryList = dictionaryService.list();
        List<CurrentUnit> currentUnitList = currentUnitService.list();
        MaintainRecord maintain = new MaintainRecord();
        return Result.OK().data("carList",carList).data("driverList",driverList).data("dictionaryList",dictionaryList).data("currentUnitList",currentUnitList).data("maintain",maintain);
    }

    @GetMapping("/findCarById/{selectCarId}/{formMainDate}")
    public Result findCarById(@PathVariable("selectCarId") Integer id , @PathVariable("formMainDate")String date) throws ParseException {
        Car carById = carService.getById(id);
        int maintainPeriod = carById.getMaintainPeriod();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date dt = sdf.parse(date);
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(dt);
        rightNow.add(Calendar.MONTH, maintainPeriod);
        Date dt1 = rightNow.getTime();
        String reStr = sdf.format(dt1);
        return Result.OK().data("time",reStr);
    }

    @GetMapping("/findNextMil/{selectCarId}/{mainMil}")
    public Result findNextMil(@PathVariable("selectCarId") int id , @PathVariable("mainMil") int mainMil){
        Car carById = carService.getById(id);
        Integer maintainMil = carById.getMaintainMil();
        int nextMil = maintainMil + mainMil;
        return Result.OK().data("nextMil",nextMil);
    }


    @PostMapping("/saveMintain")
    public Result saveMintain(@RequestBody MaintainRecord maintainRecord){
        boolean save = maintainRecordService.save(maintainRecord);
        if(save){
            return Result.OK();
        }else {
            return Result.Fail();
        }
    }

    @GetMapping("/findMaintainRecordById/{id}")
    public Result findMaintainRecordById(@PathVariable("id")int id){
        MaintainRecord byId = maintainRecordService.getById(id);
        System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++");
        return Result.OK().data("findById",byId);
    }


    @PostMapping("/updateMainRecord")
    public Result updateMainRecord(@RequestBody MaintainRecord maintainRecord){
        boolean save = maintainRecordService.updateById(maintainRecord);
        if(save){
            return Result.OK();
        }else {
            return Result.Fail();
        }
    }

    @GetMapping("/deleteSelectCheckBox/{ids}")
    public Result deleteSelectCheckBox(@PathVariable("ids") int[] ids){
        System.out.println("______________"+ids.toString());
        for(int i=0;i<ids.length;i++){
            maintainRecordService.removeById(ids[i]);
        }
        return Result.OK();
    }

    @ApiOperation("报表接口")
    @GetMapping("/reportList")
    public Result reportList(){
        return Result.OK().data("reportList",maintainRecordService.reportList());
    }
    @ApiOperation("报表按司机ID查询接口")
    @GetMapping("/queryReportByCarId/{carId}")
    public Result queryReportByCarId(@ApiParam("车辆ID") @PathVariable Integer carId){
        return Result.OK().data("reportList",maintainRecordService.queryReportByCarId(carId));
    }
}
